//
//  ChatRecordView.m
//  DoctorYL
//
//  Created by unionx on 2017/3/9.
//  Copyright © 2017年 yuntai. All rights reserved.
//

#import "ChatRecordView.h"
#import "UIImage+GIF.h"

@interface ChatRecordView ()

@property (nonatomic, strong) UIImageView *animationImageView;//可修改的图片view
@property (nonatomic, strong) UILabel *contentLabel;//显示的文本
@property (nonatomic, assign) VoiceRecordState recordState;
@property (nonatomic, strong) NSArray *imageArray;




@end

@implementation ChatRecordView


- (id)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setUpWithFrame:frame];
    }
    return self;
}

- (void)dealloc
{
}
#pragma mark - Public methods
- (void)updateRecordViewWithState:(VoiceRecordState)recordState
{
    self.recordState = recordState;
    //初始状态
    if (recordState == VoiceRecordState_Normal) {
        [self startAnimatiView];
    }else if (recordState == VoiceRecordState_Recording){
        // 正在录制
        [self startAnimatiView];
        
    }else if (recordState == VoiceRecordState_ReleaseToCancel){
        //也是录制 but 显示上滑取消UI
        self.animationImageView.image = [UIImage imageNamed:@"recordCacleBg"];
        self.contentLabel.text = @"松开手指,取消发送";
        [self stopAinmationView];
    }else if (recordState == VoiceRecordState_RecordTooShort){
        //录制时间太短呢
//        self.animationImageView.image = [UIImage imageNamed:@"recordVoiceOneBg"];
        self.contentLabel.text = @"录制时间太短了";
        UIImage *image = [UIImage sd_animatedGIFNamed:@"recordYuYinGif"];
        self.animationImageView.image = image;
        [self stopAinmationView];
        [self performSelector:@selector(selfDisappear) withObject:nil afterDelay:0.5];
    }
}
#pragma mark - Private methods

- (void)setUpWithFrame:(CGRect)frame
{
    self.imageArray =  [NSArray arrayWithObjects:[UIImage imageNamed:@"recordVoiceOneBg"],[UIImage imageNamed:@"recordVoiceTwoBg"],[UIImage imageNamed:@"recordVoiceThreeBg"],[UIImage imageNamed:@"recordVoiceFourBg"], nil] ;
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"recordVoiceBg"]];
    CGFloat baseWidth = frame.size.width;
    self.animationImageView = [[UIImageView alloc]initWithFrame:CGRectMake(38, 30, 73, 73)];
    self.animationImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:self.animationImageView];
    
    self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, baseWidth - 30 , baseWidth - 10, 20)];
    self.contentLabel.text = @"上滑取消";
    self.contentLabel.font = [UIFont systemFontOfSize:13];
    self.contentLabel.layer.masksToBounds = YES;
    self.contentLabel.layer.cornerRadius = 3;
    self.contentLabel.textColor = [UIColor whiteColor];
    self.contentLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.contentLabel];
    
}
//开始动画view
- (void)startAnimatiView
{
    self.contentLabel.text = @"手指上滑,取消发送";
//    self.contentLabel.backgroundColor = [UIColor lightGrayColor];
    UIImage *image = [UIImage sd_animatedGIFNamed:@"recordYuYinGif"];
    self.animationImageView.image = image;
//    self.animationImageView.animationImages = self.imageArray;
//    self.animationImageView.animationDuration = 1.5;
//    self.animationImageView.animationRepeatCount = 0;
//    [self.animationImageView startAnimating];
}

/**
 结束动画
 */
- (void)stopAinmationView
{
    
//    [self.animationImageView stopAnimating];
//    self.animationImageView.animationImages = nil;
//    self.contentLabel.backgroundColor = [UIColor redColor];
}
- (void)selfDisappear
{
    [self removeFromSuperview];
}





@end
